<template>
  <div>
    <q-form class="dialog_card column" ref="editRef">
      <div class="row q-col-gutter-x-md dialog_form q-pa-md">
        <div class="col-12">
          <h5><q-icon name="star" color="red" />贴子类型：</h5>
          <q-select
            outlined
            dense
            :rules="[(val) => (val && val !== null) || '请选择贴子类型']"
            v-model="isform.type_id"
            option-value="id"
            option-label="type_name"
            :options="options"
            emit-value
            map-options
            style="width: 50%"
          >
            <template v-slot:no-option>
              <q-item>
                <q-item-section class="text-grey"> No results </q-item-section>
              </q-item>
            </template>
          </q-select>
        </div>

        <div class="col-12">
          <h5><q-icon name="star" color="red" />内容类型：</h5>
          <q-select
            outlined
            dense
            :rules="[(val) => val !== null || '请选择内容类型']"
            v-model="isform.type"
            option-value="value"
            option-label="lable"
            :options="[
              { value: 1, lable: '视频' },
              { value: 2, lable: '图片' },
              { value: 3, lable: '文章' },
            ]"
            emit-value
            map-options
            style="width: 50%"
          >
          </q-select>
        </div>

        <div class="col-12">
          <h5>
            <q-icon color="red" name="star" />
            标题：
          </h5>
          <q-input
            :rules="[(val) => (val && val.length > 0) || '请输标题名称']"
            clearable
            dense
            lazy-rules
            outlined
            type="text"
            v-model="isform.title"
            style="width: 50%"
          />
        </div>

        <div class="col-12">
          <h5>
            <q-icon color="red" name="star" />
            话题：
          </h5>
          <q-input
            :rules="[(val) => (val && val.length > 0) || '请输话题名称']"
            clearable
            dense
            lazy-rules
            outlined
            type="text"
            v-model="isform.subject"
            style="width: 50%"
          />
        </div>

        <div class="col-12">
          <h5><q-icon name="star" color="red" />状态：</h5>
          <q-select
            outlined
            dense
            :rules="[(val) => val !== null || '请选择状态']"
            v-model="isform.bbs_state"
            option-value="value"
            option-label="lable"
            :options="[
              { value: 0, lable: '未审核' },
              { value: 1, lable: '审核通过' },
              { value: 2, lable: '审核拒绝' },
            ]"
            emit-value
            map-options
            style="width: 50%"
          >
          </q-select>
        </div>
      </div>
    </q-form>
  </div>
</template>

<script lang="ts" setup>
import emitter from '@/components/eventbus';
import { listAdd, listEdit } from '@/api/bbs';
import { useQuasar } from 'quasar';

interface Props {
  form: {
    id?: number; //贴子id
    type_id?: number; //类型id
    member_id?: number; //会员id
    type?: number; //内容id
    bbs_state?: number; //状态(0:未审核,1:审核通过,2:审核拒绝)
    title?: string; //标题
    subject?: string; //话题
  };
  typesoptions?: [];
}

const props = withDefaults(defineProps<Props>(), {
  form: () => ({}),
  typesoptions: () => [],
});
const editRef = ref();
const $q = useQuasar();

//接收兄弟组件传递事件
emitter.on('saveData', async () => {
  IsValidate();
});
//表单验证
const IsValidate = () => {
  if (editRef.value) {
    editRef.value.validate().then(async function (validate: any) {
      if (validate) {
        onSubmit();
      } else {
      }
    });
  }
};
//提交数据
const onSubmit = async () => {
  if (isform.value.id) {
    const res: any = await listEdit(isform.value);
    info(res);
  } else {
    const res: any = await listAdd(isform.value);
    info(res);
  }
};
//信息返回
const info = (res: any) => {
  if (res.code == 200) {
    $q.notify({
      color: 'white',
      textColor: 'positive',
      icon: 'check_circle',
      position: 'top',
      message: res.msg,
    });
    emit('backList');
  }
};

const emit = defineEmits(['update:form', 'backList']);

const isform: any = computed({
  set(value: any) {
    // emit('update:form', value);
  },
  get() {
    return props.form;
  },
});
const options: any = computed({
  set(value: any) {
    // emit('update:form', value);
  },
  get() {
    return props.typesoptions;
  },
});
</script>
<style lang="scss" scoped></style>
